Component({
  options: {
    multipleSlots: true
  },
  properties: {
    title: {
      type: String,
      value: ''
    },
    padding: {
      type: String,
      value: '12px'
    },
    ricon: {
      type: String,
      value: ''
    }
  },
  data: {
    uiShow: false,
    uiStores: [],
    uiCheckedStores: [],
    uiSelectedStoreCount: 0,
    uiSelectedStoreNames: ''
  },
  methods: {
    handleHide(e) {
      this.setData({ uiShow: false })
      this.triggerEvent('hideEvent', e)
    },
    handleShow(e, params) {
      const uiAllItem = {
        id: 'all',
        name: '所有门店',
        checked: params.reduce((prev, next) => prev && next.checked, true),
        show: true
      }

      this.setData({ uiShow: true, uiStores: [uiAllItem, ...params] })
      this.triggerEvent('showEvent', e)
    },
    handleInput(e) {
      const { value } = e.detail
      const kw = value.trim()
      const { uiStores } = this.data
      uiStores.map(item => {
        item.show = item.name.indexOf(kw) >= 0
      })

      this.setData({ uiStores })
    },
    handleCheck(e) {
      const { item } = e.currentTarget.dataset
      const { uiStores } = this.data
      const { id, checked } = item

      if (id === 'all') {
        uiStores.map(i => i.checked = !checked)
      } else {
        let one = uiStores.find(i => i.id === id)
        one.checked = !one.checked
      }

      const allItem = uiStores.find(i => i.name === '所有门店')
      const norItem = uiStores.filter(i => i.name !== '所有门店')


      allItem.checked = uiStores.filter(i => i.checked && i.name !== '所有门店').length === uiStores.length - 1

      console.log({
        uiStores,
        uiCheckedStores: norItem.filter(i => i.checked)
      })
      this.setData({
        uiStores,
        uiCheckedStores: norItem.filter(i => i.checked)
      })
    },
    handleConfirm(e) {
      // this.handleHide()
      const that = this
      this.triggerEvent('confirmEvent', {
        data: this.data.uiStores.filter(i => i.name !== '所有门店'),
        cb(e) {
          that.setData({ uiShow: false })
          // that.handleHide(e)
        }
      })
    }
  }
})
